<?php

namespace Addons\OaSystem\Controller;
use Addons\OaSystem\Controller\OaDutyBaseController;

class OaRoleController extends OaDutyBaseController
{
    var $model;

    function _initialize()
    {
        $this->model = $this->getModel('oa_role');
        parent::_initialize();

    }

    function lists(){
        $list_data = $this->_get_model_list( $this->model );
        $grids = $list_data['list_grids'];
//        $data = $list_data['list_data'];
        $data = db_select('oa_role');
        foreach ($data as $i => $v) {
            $dept = db_find('oa_dept', ['id'=>$v['dept_id']]);
            $data[$i]['dept_id'] = $dept['name'];
            $editUrl = U('edit', array('id'=> $v['id']));
            $delUrl = U('del', array('id'=> $v['id']));
            $data[$i]['urls'] = "<a href=$editUrl>编辑</a>".'   '."<a class='confirm' href=$delUrl>删除</a>";
        }

        $this -> assign('list_grids', $grids);
        $this -> assign('list_data', $data);
        $this->display();
    }

    function add(){

        if(IS_POST) {
            $data['appid'] = $this->appId;
            $data['name'] = $_POST['name'];
            $data['role_level'] = $_POST['role_level'];
            $data['role_scope'] = $_POST['role_scope'];
            $data['role_description'] = $_POST['role_description'];

            if($data['role_scope'] == 'S') {
                if(($dept = db_find('oa_dept', ['id'=>$data['dept_id']])) == NULL) {
                    $this->error(__METHOD__.'['.__LINE__.']'."角色范围为'S', 请选择部门!");
                    return false;
                }
                $data['name'] = $dept['name'].$data['name'];
                $data['dept_id'] = $_POST['dept_id'];
            }

            if(db_find('oa_role', ['name'=>$data['name'], 'dept_id'=>$data['dept_id']])) {
                $this->error('该角色名称已经存在, 请重新输入！');
                return false;
            }

            M('oa_role')->add($data);

            $this->success ( '创建' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'], $this->get_param ));
        } else {
            $depts = db_select('oa_dept');
            $this->assign('depts', $depts);
            $this->display('add');
        }

    }
}